package com.bohua.intermediary.service.impl;

import com.bohua.intermediary.common.Lay;
import com.bohua.intermediary.common.Msg;
import com.bohua.intermediary.dao.SysRoleMapper;
import com.bohua.intermediary.entity.SysDept;
import com.bohua.intermediary.entity.SysRole;
import com.bohua.intermediary.service.SysRoleService;
import com.bohua.intermediary.vo.InputTreeVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 服务实现
 * @author qian
 * @email 1415261159@qq.com
 * @date 2019/01/19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysRoleServiceImpl implements SysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public Msg addSysRole(SysRole sysRole) {
        sysRole.setCreateTime(new Date());
        sysRole.setState(1);
        sysRole.setUpdateTime(new Date());
        int i=  sysRoleMapper.insert(sysRole);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("添加成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("添加失败");
        }
        return msg;
    }

    @Override
    public Msg updateSysRole(SysRole sysRole) {
        int i= sysRoleMapper.updateByParam(sysRole);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("修改成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("修改失败");
        }
        return msg;
    }

    @Override
    public Msg delSysRole(SysRole sysRole) {
        int i=sysRoleMapper.del(sysRole.getId());
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("删除成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("删除失败");
        }
        return msg;
    }

    @Override
    public Lay getSysRoles(SysRole sysRole, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<SysRole> sysRoles=sysRoleMapper.getList(sysRole);
        PageInfo info=new PageInfo(sysRoles);
        Lay lay=new Lay();
        lay.setPage(page);
        lay.setCount(info.getTotal());
        lay.setData(info.getList());
        return lay;
    }


    @Override
    public List<InputTreeVo> getList(SysRole sysRole) {
        sysRole.setVersion(0);
        List<InputTreeVo> list=new ArrayList<>();
        List<SysRole> sysRoles=sysRoleMapper.getList(sysRole);
        for (SysRole item:
                sysRoles) {
            InputTreeVo inputTreeVo =new InputTreeVo();
            inputTreeVo.setId(item.getId());
            inputTreeVo.setName(item.getRoleName());
            inputTreeVo.setChecked(false);
            inputTreeVo.setOpen(true);
            inputTreeVo.setChildren(getChildren(item.getId()));
            list.add(inputTreeVo);
        }
        return list;
    }


    public List<InputTreeVo> getChildren(Integer parentid){
        SysRole sysRole=SysRole.builder().version(parentid).build();
        List<InputTreeVo> list=new ArrayList<>();
        /**
         * 查询父id=parentid的部门
         */
        List<SysRole> sysRoles=sysRoleMapper.getList(sysRole);
        for (SysRole item:
                sysRoles) {
            /**
             * 必然相等的条件
             */
            if (parentid.equals(item.getVersion())){
                InputTreeVo inputTreeVo =new InputTreeVo();
                inputTreeVo.setId(item.getId());
                inputTreeVo.setOpen(true);
                inputTreeVo.setName(item.getRoleName());
                inputTreeVo.setChecked(false);
                /**
                 * 新纪录的id拿出来 然后调用根据parentid的查询方法 看看还有没有新的儿子记录
                 */
                inputTreeVo.setChildren(getChildren(item.getId()));
                list.add(inputTreeVo);
            }
        }
        return list;
    }
}
